package base.shiro.demo.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 提供给普通的类调用spring管理的bean时使用
 *
 */
@Component
public class ServiceHelper implements ApplicationContextAware {

    private static ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(ServiceHelper.applicationContext == null){
            ServiceHelper.applicationContext = applicationContext;
        }
    }

    private static ApplicationContext getApplicationContext(){
        return applicationContext;
    }

    /**
     * 静态的获取Spring管理bean实例的方法
     *
     * @param serviceName
     *            bean的名称
     * @return 返回bean的实体对象
     */
    public static  Object getService(String serviceName){
        return getApplicationContext().getBean(serviceName);
    }
}
